AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( 'shared.lua' )

function ENT:SpawnFunction( ply, tr )
	if ( !tr.Hit ) then return end
	local SpawnPos = tr.HitPos + tr.HitNormal * 16
	local ent = ents.Create("sent_hypermass_extractor")
	ent:SetPos(SpawnPos)
	ent:Spawn()
	ent:Activate()
	return ent
end

function ENT:Initialize()
	self.Entity:SetModel("models/props_canal/canal_cap001.mdl")
	self.Entity:PhysicsInit(SOLID_VPHYSICS)
	self.Entity:SetMoveType(MOVETYPE_VPHYSICS)
	self.Entity:SetSolid(SOLID_VPHYSICS)
	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
	end
        RD_Register(self, true)--For Environments Compatibility

	self.NextThink = 0
end

function ENT:Think()
	if self.NextThink > CurTime() then return end
	local radius = ents.FindInSphere(self.Entity:GetPos(),5000)--Was 50
	for k,v in pairs(radius) do
		if v:IsValid() then
			if string.find(v:GetClass(), "sent_spaceanon_hypermass") then
				self:MakeSparks(v)
				self:SupplyResource("energy", 2500)
			end			
		end
	end
	self.NextThink = CurTime()+1
end

function ENT:MakeSparks()
	i = math.random(1,4)
	local pos = self.Entity:GetPos()+(self.Entity:GetForward()*0)+(self.Entity:GetRight()*-35)+(self.Entity:GetUp()*-35)
	if i == 2 then
		pos = self.Entity:GetPos()+(self.Entity:GetForward()*0)+(self.Entity:GetRight()*-35)+(self.Entity:GetUp()*35)
	elseif i == 3 then
		pos = self.Entity:GetPos()+(self.Entity:GetForward()*0)+(self.Entity:GetRight()*35)+(self.Entity:GetUp()*-35)
	elseif i == 4 then
		pos = self.Entity:GetPos()+(self.Entity:GetForward()*0)+(self.Entity:GetRight()*35)+(self.Entity:GetUp()*35)
	end
	spark = ents.Create("point_tesla")
	spark:SetKeyValue("targetname", "teslab")
	spark:SetKeyValue("m_SoundName" ,"DoSpark")
	spark:SetKeyValue("texture" ,"sprites/plasma.spr")
	spark:SetKeyValue("m_Color" , math.random(0,255).." "..math.random(0,255).." "..math.random(0,255))
	spark:SetKeyValue("m_flRadius" , "250")
	spark:SetKeyValue("beamcount_min", "5")
	spark:SetKeyValue("beamcount_max", "30")
	spark:SetKeyValue("thick_min", "15")
	spark:SetKeyValue("thick_max", "40")
	spark:SetKeyValue("lifetime_min" ,"0.1")
	spark:SetKeyValue("lifetime_max", "0.2")
	spark:SetKeyValue("interval_min", "0.05")
	spark:SetKeyValue("interval_max" ,"0.08")
	spark:SetPos(pos)
	spark:Spawn()
	spark:Fire("DoSpark","",0)
	spark:Fire("kill","", .25)
end